package Converter;

import VO.Klasse;
import VO.OppgaveSett;
import DB.DBehandler;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import Hjelpeklasser.VariabelSamarbeid;

@ManagedBean
@SessionScoped
/**
 * OppgaveSettConverter; bruker DBehandler
 */
public class OppgaveSettConverter implements Converter, Serializable {
    private DBehandler db = new DBehandler();
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        VariabelSamarbeid varCoop = new VariabelSamarbeid();
        String op = varCoop.getOperator();
        Klasse kl = varCoop.getKlasse();
        ArrayList<OppgaveSett> os = db.hentOppgSett(op,kl);
        
        for (OppgaveSett a : os) {
            if (a.getNivaa() == Integer.parseInt(value)) {
                return a;
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        OppgaveSett o = (OppgaveSett) value;
        return o.toString();
    }
}